package com.jvyou.mybatis.utils.clazz;

import com.google.common.collect.Lists;
import com.google.common.reflect.ClassPath;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ClassUtils {

    // 私有构造函数，防止实例化
    private ClassUtils() {
    }

    // 将指定的全限定类名加载为Class对象
    public static Class<?> getClass(String className) throws ClassNotFoundException {
        if (className == null || className.trim().isEmpty()) {
            throw new IllegalArgumentException("Class name must not be null or empty");
        }
        // 加载类
        return Class.forName(className);
    }

    public static List<Class<?>> loadClasses(ClassLoader classLoader, List<String> packageNames) throws IOException {
        List<Class<?>> classes = new ArrayList<>();
        // 从类路径中获取 ClassPath 对象
        ClassPath classPath = ClassPath.from(classLoader);
        // 获取包下的所有类
        for (String packageName : packageNames) {
            for (ClassPath.ClassInfo classInfo : classPath.getTopLevelClasses(packageName)) {
                classes.add(classInfo.load());
            }
        }
        return classes;
    }

    public static List<Class<?>> loadClasses(ClassLoader classLoader, String... packageNames) throws IOException {
        // 使用Guava将packageNames转换为List
        return loadClasses(classLoader, Lists.newArrayList(packageNames));
    }
}
